package cn.wp.common.core.plugin.scheduler;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.List;
import java.util.concurrent.ScheduledFuture;

/**
 * 任务信息
 * Created by chenyb on 2019/10/23
 */
@Data
public class SchedulerTask {

    // 初始化
    private boolean isInit = true;

    // 是否测试任务
    private boolean isTest = false;

    // 任务ID
    private String taskId;

    // 任务名称
    private String taskName;

    // cron表达式
    private String cron;

    // 最后一次开始运行时间
    private String lastStartRunDatetime;

    // 最后一次运行时间
    private String lastEndRunDatetime;

    // 最后一次运行耗时
    private long lastCost;

    // 最后一次运行是否成功  0-成功  1-不成功
    private int lastRunSuccess;

    // 下次运行时间
    private List<String> nextRunTimeList;

    // 状态 0-停止状态  1-运行状态 2-等待下次运行
    private int status;

    // 状态 0-停止状态  1-运行状态 2-等待下次运行
    private String statusText;

    // 调度实例
    @JsonIgnore
    private ScheduledFuture<?> future;

    // 线程实例
    @JsonIgnore
    private SchedulerRunnable runnable;

    public String getStatusText() {
        if(status == 0) {
            statusText = "未运行";
        }
        if(status == 1) {
            statusText = "运行中";
        }
        if(status == 2) {
            statusText = "等待下次触发运行";
        }
        return statusText;
    }

    @Override
    public String toString() {
        return "SchedulerTask{" +
                "taskId='" + taskId + '\'' +
                ", taskName='" + taskName + '\'' +
                ", cron='" + cron + '\'' +
                ", lastStartRunDatetime='" + lastStartRunDatetime + '\'' +
                ", lastEndRunDatetime='" + lastEndRunDatetime + '\'' +
                ", lastCost=" + lastCost +
                ", lastRunSuccess=" + lastRunSuccess +
                ", nextRunTimeList=" + nextRunTimeList +
                ", status=" + status +
                ", statusText='" + statusText + '\'' +
                '}';
    }
}
